package com.yilin.tms.data.controller;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.gson.reflect.TypeToken;
import com.yilin.commons.util.JsonUtil;
import com.yilin.tms.core.application.PageData;
import com.yilin.tms.core.application.ReturnData;
import com.yilin.tms.core.commons.annotation.SkipLoginVerify;
import com.yilin.tms.core.commons.base.BaseController;
import com.yilin.tms.core.commons.utils.ParamUtil;
import com.yilin.tms.core.entity.capital.BankCard;
import com.yilin.tms.core.entity.capital.PayChannel;
import com.yilin.tms.core.entity.capital.TradeRecord;
import com.yilin.tms.core.entity.data.contracted.ContractBody;
import com.yilin.tms.core.entity.data.document.Document;
import com.yilin.tms.core.entity.data.document.DocumentResource;
import com.yilin.tms.core.entity.data.monitor.TmsRootData;
import com.yilin.tms.core.entity.data.receipt.ReceiptOrder;
import com.yilin.tms.core.entity.order.transit.TransitOrderNote;
import com.yilin.tms.core.remote.IOrderRemote;
import com.yilin.tms.data.mapper.*;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.persistence.Transient;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/data/migration/")
public class MigrationController extends BaseController {
    @Resource
    TmsRootDataMapper tmsRootDataMapper;
    @Resource
    ContractMapper contractMapper;
    @Resource
    ReceiptOrderMapper receiptOrderMapper;
    @Resource
    DocumentMapper documentMapper;
    @Resource
    DocumentResourceMapper documentResourceMapper;
    @Resource
    IOrderRemote orderRemote;


    /**
     * 省运管数据
     * */
    @PostMapping("tmsRootDataList")
    @SkipLoginVerify
    public ReturnData<Object> tmsRootDataList(int page) {
        PageData<TmsRootData> data = tmsRootDataMapper.selectPage(new PageData<>(page, 5000),new QueryWrapper<TmsRootData>().orderByAsc("create_time"));
        return jsonView(SUCCESS_CODE, "移植完成",data.getList());
    }

    /**
     * 获取所有合同
     * */
    @PostMapping("contractList")
    @SkipLoginVerify
    public ReturnData<Object> contractList(int page) {
        PageData<ContractBody> data = contractMapper.selectPage(new PageData<>(page, 5000),new QueryWrapper<ContractBody>().orderByAsc("create_time"));
        return jsonView(SUCCESS_CODE, "移植完成",data.getList());
    }

    /**
     * 获取所有回执单
     * */
    @PostMapping("receiptOrderList")
    @SkipLoginVerify
    public ReturnData<Object> receiptOrderList(int page) {
        PageData<ReceiptOrder> data = receiptOrderMapper.selectPage(new PageData<>(page, 5000),new QueryWrapper<ReceiptOrder>().orderByAsc("create_time"));
        String toJson = JsonUtil.objectToJson(data.getList());
        List<Map<String, Object>> dataList = JsonUtil.jsonToObject(toJson, new TypeToken<List<Map<String, Object>>>() {});
        for (Map<String, Object> map : dataList) {
            TransitOrderNote note = ParamUtil.isSuccess(orderRemote.getTransitOrderNoteById((String) map.get("transitOrderNoteId")));
            map.computeIfAbsent("totalDeliverMeasure", k -> note.getNoteDeliverMeasure());
            map.computeIfAbsent("totalReceiptMeasure", k -> note.getNoteReceiptMeasure());
            map.computeIfAbsent("measureUnit", k -> note.getMeasureUnit());
            map.putIfAbsent("receiptSeal", "http://117.187.230.142:9100/auth/seal_ADDA1A09553F4512B315382442044C6E.png");
        }
        return jsonView(SUCCESS_CODE, "移植完成",dataList);
    }

    /**
     * 获取所有文档节点
     * */
    @PostMapping("documentList")
    @SkipLoginVerify
    public ReturnData<Object> documentList(int page) {
        PageData<Document> data = documentMapper.selectPage(new PageData<>(page, 5000),new QueryWrapper<Document>().orderByAsc("create_time"));
        return jsonView(SUCCESS_CODE, "移植完成",data.getList());
    }

    /**
     * 获取所有文档内容
     * */
    @PostMapping("documentResourceList")
    @SkipLoginVerify
    public ReturnData<Object> documentResourceList(int page) {
        PageData<DocumentResource> data = documentResourceMapper.selectPage(new PageData<>(page, 5000),new QueryWrapper<DocumentResource>().orderByAsc("create_time"));
        return jsonView(SUCCESS_CODE, "移植完成",data.getList());
    }


}
